﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Threading.Tasks;
using System.Xml.Linq;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;


namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsstoreServices : Repository<corecmsstore>, IcorecmsstoreServices
    {
        #region 实现重写增删改查操作==========================================================

        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public bool InsertAsync(corecmsstore entity)
        {
            return base.Change<corecmsstore>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsstore> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsstore entity)
        {
            return base.Change<corecmsstore>().Update(a => new corecmsstore()
            {
                mainStore = entity.mainStore,
                corpId = entity.corpId,
                splitId = entity.splitId,
                splitGroupId = entity.splitGroupId,
                storeName = entity.storeName,
                mobile = entity.mobile,
                linkMan = entity.linkMan,
                logoImage = entity.logoImage,
                earnestAmount = entity.earnestAmount,
                earnestBalance = entity.earnestBalance,
                areaId = entity.areaId,
                cityId = entity.cityId,
                provinceId = entity.provinceId,
                address = entity.address,
                coordinate = entity.coordinate,
                latitude = entity.latitude,
                longitude = entity.longitude,
                isDefault = entity.isDefault,
                createTime = entity.createTime,
                updateTime = entity.updateTime,
                distance = entity.distance,
                openingTime = entity.openingTime,
                closingTime = entity.closingTime,
                appId = entity.appId,
                appSecret = entity.appSecret,
                payAccount = entity.payAccount,
                mallType = entity.mallType,
                sharingCode = entity.sharingCode,
                deviceId = entity.deviceId,
                deviceName = entity.deviceName,
                isDelete = entity.isDelete
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsstore> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsstore>().Update(a => new corecmsstore()
                {
                    mainStore = item.mainStore,
                    corpId = item.corpId,
                    splitId = item.splitId,
                    splitGroupId = item.splitGroupId,
                    storeName = item.storeName,
                    mobile = item.mobile,
                    linkMan = item.linkMan,
                    logoImage = item.logoImage,
                    earnestAmount = item.earnestAmount,
                    earnestBalance = item.earnestBalance,
                    areaId = item.areaId,
                    cityId = item.cityId,
                    provinceId = item.provinceId,
                    address = item.address,
                    coordinate = item.coordinate,
                    latitude = item.latitude,
                    longitude = item.longitude,
                    isDefault = item.isDefault,
                    createTime = item.createTime,
                    updateTime = item.updateTime,
                    distance = item.distance,
                    openingTime = item.openingTime,
                    closingTime = item.closingTime,
                    appId = item.appId,
                    appSecret = item.appSecret,
                    payAccount = item.payAccount,
                    mallType = item.mallType,
                    sharingCode = item.sharingCode,
                    isDelete = item.isDelete
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsstore>().Update(a => new corecmsstore()
            {
                isDelete = true,
                updateTime = DateTime.Now
            }, a => a.id == id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsstore>().Update(a => new corecmsstore()
                {
                    isDelete = true,
                    updateTime = DateTime.Now
                }, a => a.id == id);
            }
            return returnKey;
        }

        #endregion

        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsstore> GetCaChe()
        {
            return base.GetList();
        }

        #endregion

        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsstore> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsstore> lst = new List<corecmsstore>();
            var where = Expressionable.Create<corecmsstore>();
            where = where.And(a => a.isDelete == false);
            if (!string.IsNullOrEmpty(sName))
            {
                where = where.And(a => a.storeName.Contains(sName))
                    .Or(a => a.storeName.Contains(sName));
            }
            if (pageIndex >= 1 && pageSize > 0)
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .OrderBy(a => a.storeName).ToList();
            }
            else
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.storeName).ToList();
            }

            return lst;
        }
        #endregion

    }
}
